﻿using AutoMapper;
using BaseData.CommonLib.Models;
using BaseData.DomainLib.AggregatesModel.WorkstationAggregate;
using MediatR;
using Swimj.CommonLib.Base;

namespace BaseData.ApplicationLib.Commands;

public class UpdateWorkstationCommand: WorkstationEditRequestDto, IRequest<Result<WorkstationInfoResponseDto>>
{
    public int Id { get; set; }
}

public class UpdateWorkstationCommandHandler : IRequestHandler<UpdateWorkstationCommand, Result<WorkstationInfoResponseDto>>
{

    private readonly IWorkstationRepository _workstationRepository;
    private readonly IMapper _mapper;

    public UpdateWorkstationCommandHandler(IWorkstationRepository workstationRepository,
        IMapper mapper)
    {
        this._workstationRepository = workstationRepository;
        this._mapper = mapper;
    }

    public async Task<Result<WorkstationInfoResponseDto>> Handle(UpdateWorkstationCommand request, CancellationToken cancellationToken)
    {
        var workstation = await _workstationRepository.Get(request.Id);
        workstation?.Update(request.Name, request.Description);

        await _workstationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

        return await Result<WorkstationInfoResponseDto>.SuccessAsync(_mapper.Map<WorkstationInfoResponseDto>(workstation));
    }
}